using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using mshtml;

namespace Skinner
{
    public partial class SkinnerMain : Form
    {

        private HtmlDocument _document = null;
        private HTMLDocumentClass _htmlDoc;
        private List<StyleSheet> _cssSheets = new List<StyleSheet>();
        ColorPalette _colorPalette = null;
        ImagePalette _imagePalette = null;
        StylePalette _stylePalette = null;
        StyleSheet _selectedSheet = null;

        public StyleSheet SelectedSheet
        {
            get { return _selectedSheet; }
        }

        public SkinnerMain()
        {
            InitializeComponent();
        }

        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //empty the existing collection of sheets
            _cssSheets.Clear();

            _document = browser.Document;
            _htmlDoc = (HTMLDocumentClass)_document.DomDocument;
            InspectStyles();
            BlockNavigation();
            if (_cssSheets.Count != 0)
            {
                if (cboStyles.SelectedIndex >= 0)
                {
                    _selectedSheet = _cssSheets[cboStyles.SelectedIndex];
                }
                else
                {
                    _selectedSheet = _cssSheets[0];
                }
                ShowColorPalette();
            }
        }

        private void InspectStyles()
        {
            foreach (mshtml.IHTMLStyleSheet styleSheet in _htmlDoc.styleSheets)
            {
                _cssSheets.Add(new StyleSheet(styleSheet));
            }
            cboStyles.DataSource = null;
            cboStyles.DataSource = _cssSheets;
            cboStyles.DisplayMember = "HREF";
            cboStyles.ValueMember = "HREF";
            cboStyles.Enabled = true;
            
        }

        private void cmdGo_Click(object sender, EventArgs e)
        {
            try
            {
                this.browser.Navigate(txtAddress.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("An error has occurred. " + ex.ToString());
            }
        }

        private void cmdInspect_Click(object sender, EventArgs e)
        {
            if (browser.Document != null)
            {
                BlockNavigation();
                InfoPopup frm = new InfoPopup(this);
                frm.Show();
            }
        }

        private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (blockNavigation.Checked)
            {
                e.Cancel = true;
                return;
            }

            if (_selectedSheet != null)
            {
                if (MessageBox.Show("Make sure you have saved your work before navigating away from this page! \n\nIf you want to see how your edits apply to other pages on this site, save your work before navigating and reload the work once navigation is complete.", "Go to a new page?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                    e.Cancel = true;
            }
        }

        private void cboStyles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboStyles.SelectedIndex >= 0)
            {
                _selectedSheet = _cssSheets[cboStyles.SelectedIndex];
                if (viewColorsToolStripMenuItem.Checked == true)
                {
                    ShowColorPalette();
                }
                else if (viewImagesToolStripMenuItem.Checked == true)
                {
                    ShowImagePalette();
                }
                else if (viewMatchingStylesToolStripMenuItem.Checked == true)
                {
                    ShowStylePalette();
                }
            }
        }

        private void ShowColorPalette()
        {
            if (browser.Document == null) return;
            ClearPanel();
            _colorPalette = new ColorPalette(_selectedSheet);
            _colorPalette.Location = new Point(0, 0);
            splitContainer1.Panel2.Controls.Add(_colorPalette);
        }

        private void ShowImagePalette()
        {
            if (browser.Document == null) return;
            ClearPanel();
            _imagePalette = new ImagePalette(_selectedSheet, this.browser.Document);
            _imagePalette.Location = new Point(0, 0);
            splitContainer1.Panel2.Controls.Add(_imagePalette);
        }
        
        private void ShowStylePalette()
        {
            if (browser.Document == null) return;
            ClearPanel();
            _stylePalette = new StylePalette(_selectedSheet, browser.Document);
            _stylePalette.Location = new Point(0, 0);
            splitContainer1.Panel2.Controls.Add(_stylePalette);
        }
        
        private void ClearPanel()
        {
            splitContainer1.Panel2.Controls.Clear();
            if (_colorPalette != null)
            {
                _colorPalette.Dispose();
                _colorPalette = null;
            }
            if (_imagePalette != null)
            {
                _imagePalette.Dispose();
                _imagePalette = null;
            }
            if (_stylePalette != null)
            {
                _stylePalette.Dispose();
                _stylePalette = null;
            }
        }

        private void viewImagesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (browser.Document == null) return;
            if (_imagePalette == null)
            {
                viewImagesToolStripMenuItem.Checked = true;
                viewColorsToolStripMenuItem.Checked = false;
                viewMatchingStylesToolStripMenuItem.Checked = false;
                ShowImagePalette();
            }
        }

        private void viewColorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (browser.Document == null) return;
            if (_colorPalette == null)
            {
                viewColorsToolStripMenuItem.Checked = true;
                viewImagesToolStripMenuItem.Checked = false;
                viewMatchingStylesToolStripMenuItem.Checked = false;
                ShowColorPalette();
            }
        }

        private void viewMatchingStylesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (browser.Document == null) return;
            if (_stylePalette == null)
            {
                viewMatchingStylesToolStripMenuItem.Checked = true;
                viewImagesToolStripMenuItem.Checked = false;
                viewColorsToolStripMenuItem.Checked = false;
                ShowStylePalette();
            }
        }

        private void blockNavigation_Click(object sender, EventArgs e)
        {
            SetNavigation();
        }

        private void SetNavigation()
        {
            if (blockNavigation.Checked)
            {
                AllowNavigation();
            }
            else
            {
                BlockNavigation();
            }
        }

        private void BlockNavigation()
        {
            blockNavigation.Checked = true;
            blockNavigation.Image = global::Skinner.Properties.Resources.redstop;
            blockNavigation.Text = "Navigation Blocked";
        }

        private void AllowNavigation()
        {
            blockNavigation.Checked = false;
            blockNavigation.Image = global::Skinner.Properties.Resources.greengo;
            blockNavigation.Text = "Navigation Allowed";
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_selectedSheet == null)
                return;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter file = new StreamWriter(saveFileDialog.FileName);
                file.Write(_selectedSheet.Sheet.cssText);
                file.Close();
                file.Dispose();
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_selectedSheet == null)
                return;

            if (MessageBox.Show("This operation will take a css file and overlay it on whatever css you have selected. If the styles don't match, who knows what sort of errors will happen.", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    StreamReader file = new StreamReader(openFileDialog.FileName);
                    _cssSheets.Clear();
                    _selectedSheet.Sheet.cssText = file.ReadToEnd();
                    InspectStyles();
                    file.Close();
                    file.Dispose();
                    cboStyles_SelectedIndexChanged(null, null);
                }
            }
        }

        private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_selectedSheet == null)
                return;

            if (MessageBox.Show("This operation will take all modifications to the current sheet and make a theme. \n\nYou can't reload the theme using this tool unless you apply it to a site and open the site using this tool. If what you really want to do is save your work for later, use the save option instead.", "Create Theme?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                MakeTheme maker = new MakeTheme(_selectedSheet, browser.Document);
                maker.ShowDialog();
            }
        }
    }   
}